import publicForm from "@/components/public/el-forms.vue"; //弹框form
import formJs from "./regexDetail.js"; //新增弹框配置项
import {
    saveCheckitems, //保存
    getCheckitemsByFid, //详情


} from "@/api/request.js";
export default {
    props: [
        "dialogVisible",
        "dialogTitle",
        "statusNum",
        "currentRow",
        "fCompanyid"
    ],
    data() {
        return {
            visible: "",
            loading:false,
            title: null,
            isLock: false,

            btnStatus: [],
            dialogHeight: null,
            activeIndex: "1",

            currntData: null,
            dataInfo: null,
            startDates: null,
            treeData: [], //材料种类
            formData: {
                fId: "", //主键id
                fChecktype: "", //树状图类型
                fItemno: "", //标准代号
                fItemname: "", //标准名称
                fItemvalue: "", //树状图类型id
                fNote: "", //备注
                userId: "", //当前登录用户id
                userName: "", //当前登录用户姓名
            },
            formObj: formJs.formObj,
            formDatafileList: [], //工作计划资料
            btnShow: true,
            currentDetail: null,
            selected: [],
            fCompanyids: null
        };
    },
    components: {
        publicForm,
    },
    watch: {
        dialogVisible: {
            handler(newVal) {
                if (newVal) {
                    this.visible = newVal;
                }
            },
            immediate: true,
        },
        dialogTitle: {
            handler(newVal) {
                if (newVal) {
                    this.title = newVal;
                }
            },
            immediate: true,
        },
        fCompanyid: {
            handler(newVal) {
                if (newVal) {
                    this.fCompanyids = newVal
                }
            },
            immediate: true,
        },
        statusNum: {
            handler(newVal) {
                if (newVal) {

                    this.edit(newVal);
                }
            },
            immediate: true,
        },

        currentRow: {
            handler(newVal) {
                if (newVal) {
                    this.currentDetail = newVal;
                    if (this.statusNum != 1) {
                        this.getDetail({
                            fId: this.currentDetail.fId
                        }) // currentRow
                    }
                }
            },
            immediate: true,
        },

    },
    created() {
        window.addEventListener("resize", this.getHeight);
        this.getHeight();
    }, //获取页面高度自适应：

    destroyed() {
        window.removeEventListener("resize", this.getHeight);
        this.getHeight();
    },
    mounted() {
        this.userInfo = JSON.parse(sessionStorage.getItem("yjSmart-userInfo"));
        this.btnShow = true
        
        if (this.statusNum == 1) { //新增
            this.formData = this.$options.data.call(this).formData;
            this.btnShow = true
            this.formObj.map((item) => {
                item.disabled = false;
            });
            this.btnShow = true

        } else if (this.statusNum == 2) { //编辑
            // currentRow
            this.formData.fId = this.currentDetail.fId //按钮 
            this.getDetail({
                fId: this.currentDetail.fId
            }) // currentRow
            this.formObj.map((item) => {
                item.disabled = false;
            });
            this.btnShow = true
            //编辑
            // this.formObj.map((item) => {
            //     item.disabled = false;
            // });
            // this.formObj[2].disabled = true;
            // this.btnShow = true
        } else if (this.statusNum == 7) { //查看
            this.btnShow = false
            this.formObj.map((item) => {
                item.disabled = true;
            });
        }
        this.getHeight();
    },
    methods: {
        getHeight() {
            var h = document.documentElement.clientHeight; //获取页面可见高度
            this.dialogHeight = h * 0.3 + "px";
        },
        handleSelect(key) {
            this.activeIndex = key;
        },
        closeDialog() {
            this.$emit("close");
        },
        selectChange(data, a, b) {
           console.log(data, a, b);

        },
        //   edit(status)
        edit() {
            this.formData = this.$options.data.call(this).formData;

            // if (status == 1) {
            //     this.formData = this.$options.data.call(this).formData;
            // } else if (status == 2) {
            //     this.formData = this.$options.data.call(this).formData;
            // } else if (status == 4) {
            //     this.monthExamineData = this.$options.data.call(this).monthExamineData;
            // }
        },
   
        // 详情
        getDetail(obj) {
            this.loading=true;
            getCheckitemsByFid(obj).then((res) => {
                if (res.errcode == 0) {
                    this.loading=false;
                    var data = res.data.lrCheckCheckitems
                    this.formData = data;
                }else {
                    this.loading=false;
                    this.$message.error(res.msg);
                }
            });
        },

        yj_saveForm(status) {
            this.formData.userId = this.userInfo.userId; //当前登录人id
            this.formData.userName = this.userInfo.realName; //当前登录人名称
           
            this.formData.fItemvalue =   this.fCompanyids
            // 0连续新增1新增
            // this.formData
            if (this.$refs.publicForm.submitForm()) {
                this.saveForm(this.formData, status);
            } else {
                console.log("error");
            }

        },
        // 保存
        saveForm(data, status) {
            this.loading = true;
            this.isLock = true
            saveCheckitems(data)
                .then((res) => {
                    if (res.errcode == 0) {
                        this.loading = false;
                        this.visible = false;
                        this.$emit("refreshList", status);
                        this.$message.success("新增成功");
                        this.isLock = false
                    }else {
                        this.loading = false;
                        this.isLock = false
                        this.$message.error(res.msg);

                    }
                })


        },
    },
};